package eg.backend.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.datanucleus.jpa.annotations.Extension;

import eg.backend.base.UserType;

@Entity
@NamedQueries({
	@NamedQuery(name = "User.getByKey", query="select t from eg.backend.domain.User t where t.id=:id"),
	@NamedQuery(name = "User.findByName", query = "select u from eg.backend.domain.User u where u.username.startsWith(:name)"),
	@NamedQuery(name = "User.getByEmail", query = "select u from eg.backend.domain.User u where u.email = :email"),
	@NamedQuery(name = "User.findByUserType", query = "select u from eg.backend.domain.User u where u.type = :type")
	
})
public class User {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Extension(vendorName = "datanucleus", key = "gae.encoded-pk", value = "true")
	private String id;
	
	private String email;
	private String username;
	private String password;
	private boolean enabled = true;
	
	@Enumerated(EnumType.STRING)
	private UserType type;

	//hold user's reference to system dimensions, both mandatory and optional
	@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner", fetch = FetchType.EAGER)
	private Set<DimensionPermission> dimensionPermissions = new HashSet<DimensionPermission>();
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public UserType getType() {
		return type;
	}

	public void setType(UserType type) {
		this.type = type;
	}

	public void disable() {
		this.enabled = false;
	}
	
	public boolean isEnabled() {
		return enabled;
	}

	public Set<DimensionPermission> getDimensionPermissions() {
		return dimensionPermissions;
	}

	public void setDimensionPermissions(
			Set<DimensionPermission> dimensionPermissions) {
		this.dimensionPermissions = dimensionPermissions;
	}

	public void addDimensionPermission(DimensionPermission dimensionPermission) {
		this.dimensionPermissions.add(dimensionPermission);
	}

	public void deleteDimensionPermission(DimensionPermission dimensionPermission) {
		this.dimensionPermissions.remove(dimensionPermission);
	}
	
	@Override
	public int hashCode() {
		return new HashCodeBuilder().append(this.email).toHashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		if (!this.getClass().isInstance(obj)) {
			return false;
		}

		User user = (User) obj;
		return new EqualsBuilder().append(this.email, user.getEmail())
				.isEquals();
	}
}
